import { Component } from '@angular/core';
import {NavController} from 'ionic-angular';
import {Device} from "ionic-native";
import {AppService, AppGlobal} from "../../app/app.service";
import { Buffer } from 'buffer';
import { Md5 } from 'ts-md5/dist/md5';
/**
 * Generated class for the LoginPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  passwdflg: boolean = false;
  show_psd: boolean = false;
  user: any;
  showflg: boolean = false;
  errormsg: any = "账号输入有误！";

  constructor(public navCtrl: NavController,public appService: AppService) {
    console.log('Device uuid is: ' + Device.uuid);//获取设备的通用唯一标识符（UUID），一个UUID生成由设备制造商确定和设备特定的平台或模型。
    console.log('Device cordova is: ' + Device.cordova);//获取cordova版本
    console.log('Device version is: ' + Device.version);//获取系统版本
    console.log('Device manufacturer is: ' + Device.manufacturer);//得到设备的制造商
    console.log('Device model is: ' + Device.model);//获取设备模型或产品名称。该值由设备制造商设置，并可在同一产品的版本中不同。
    console.log('Device platform is: ' + Device.platform);//获取运行平台
    console.log('Device serial is: ' + Device.serial);//设备硬件序列号
    console.log('Device isVirtual is: ' + Device.isVirtual);//设备是否运行在模拟器
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }

  logIn(username: HTMLInputElement, password: HTMLInputElement){
	  if(username.value.length == 0) {
      this.showflg = true;
      this.errormsg = "请输入用户名";
	  }else if(password.value.length != 6) {
      this.showflg = true;
      this.errormsg = "请输入密码";
	  }else {
      this.showflg = false;
      this.user = JSON.parse(localStorage.getItem('user'));
      this.user = this.user ? JSON.parse(localStorage.getItem('user')) : [];
      console.log(this.user);
      let sessiontoken = this.user.sessiontoken;
      if (sessiontoken === undefined){
        sessiontoken = null;
      }
      let sysuuid = Device.uuid;
      let passwd = new Buffer(encodeURIComponent(password.value)).toString('base64');
      let md5 = Md5.hashStr(username.value + passwd + sysuuid).toString();
      var params = {user_id: username.value, passwd: passwd, sessiontoken: sessiontoken, sysuuid: sysuuid,md5:md5,
        sysversion:Device.version,manufacturer:Device.manufacturer,model:Device.model,platform:Device.platform,
        serial:Device.serial,isVirtual:Device.isVirtual};
      let succ = 0;
      this.appService.httpPost(AppGlobal.API.login ,params, rs => {
        console.debug(rs);
        this.user = rs.data;
        succ = rs.state;
        console.debug(succ);
        if (succ){
          console.debug(succ);
          this.showflg = false;
          if (this.passwdflg){
            localStorage.setItem('passwd',password.value);
            localStorage.setItem('username',password.value);
            localStorage.setItem('loginsts',"1");//登录状态1-已登录0-退出
            localStorage.setItem('user',JSON.stringify(this.user));//保存用户信息
          } else {
            localStorage.removeItem('passwd');
            localStorage.removeItem('username');
            localStorage.removeItem('loginsts');
            localStorage.removeItem('user');
          }
          //let modal = this.modalCtrl.create(TabsPage);
         //modal.present();
          //this.navCtrl.setRoot(TabsPage);
          this.navCtrl.pop();
        }else {
          this.showflg = true;
          this.errormsg = rs.message;
        }
      });
	  }
	}
  logOn(){
    this.navCtrl.push('LogonPage');
  }
  pushNotificationChange(){
      this.passwdflg = !this.passwdflg;
  }

  showPassword() {
    this.show_psd = !this.show_psd;
  }
}


